<?php
   class Cart extends MY_Controller{
	   public function __construct(){
		   parent::__construct();
		   $this->load->helper("url");
		   $this->load->library('cart');
		   $this->load->helper('form');
		   $this->load->model("model_cart");
		   $this->load->model("model_home");
		   //$this->load->model("model_cruise");
	   }
	   public function index(){
		   $this->load->view("public/cart"); 
	   }
	   public function add(){
		   if(!$_POST){die("false");}
		   $id = $_POST['id']; $type = $_POST['type'];
		   $val = $_POST['val']; $name = $_POST['name'];
		   $depart = $_POST['depart']; $return = $_POST['returns'];
		   $qty = $_POST['number']; $train = $_POST['trainame'];
		   $from = $_POST['from']; $to = $_POST['to'];
		   $data = $this->model_cart->gettick($id);
		   $shop = array(
			   'id'      => $id,
			   'qty'     => $qty,
			   'price'   => $val,
			   'name'    => $name,
			   'options' => array(
			   		'trainid' => $data['trainid'],
			   	    'train' => $train,
					'hdepart' => $data['tickdeparts'],
					'hreturn' => $data['tickarrives'],
					'htotal' => $data['tickduration'],
					'from' => $from,'to'=>$to,
					'type'  => $type,
					'depart'  => $depart,
					'return'    => $return
				)
			);
			$this->cart->insert($shop);	
		    echo $this->cart->total();
	   }
	   public function view(){
		  $data['cart'] = $this->cart->contents();
		  //$this->debug($data['cart']);
		  $ship_id = "";
		  $adults  = array();
		  $childs  = array();
		  $infants = array();
		  foreach($data['cart'] as $items){
			  $ship_id   = $items['options']['ship']; 
			  $adults[]  = $items['options']['adults'];
			  $childs[]  = $items['options']['child'];
			  $infants[] = $items['options']['infants'];
			  //break;
		  }
		  $total_adults = array_sum($adults);
		  $total_childs = array_sum($childs);
		  $total_infant = array_sum($infants);
		  $data['services'] =  $this->model_cart->getservice($ship_id);
		  $data['adults']   =  (int)$total_adults;
		  $data['childs']   =  (int)$total_childs;
		  $data['infants']  =  (int)$total_infant;
		  $data['result'] =  $this->model_cruise->getdata($ship_id);
		  //$this->debug($data['services']);
		  $data['template'] = "cart/content";
		  $data['title'] = "Your cart - Halong Bay Cruise - Cruise in Vietnam";
		  $this->load->view("cart/layout",$data);	   
	   }
	   public function updatecart(){
			for ($i=1;$i<=$this->cart->total_items();$i++){
		        $item = $this->input->post($i);
		        $data = array(
		                'rowid'    => $item['rowid'],
		                'qty'    => $item['qty']
				);
		        $this->cart->update($data);
			}
			redirect(base_url().'home/cart/view','refresh');		
		
		}
	   public function del(){
		  	$getshop=$this->cart->contents();
			foreach($getshop as $item){
				if($item['id'] == $this->uri->segment(4)){
					$data['rowid']=$item['rowid'];
					$data['qty'] = 0;
			        $this->cart->update($data);
					break;
				}
			}
			redirect(base_url().'your-cart#infocruise','refresh');	
	   }
	   public function emptycart(){
		   $this->cart->destroy();	
		   redirect(base_url()."home/cart");
	   }
	   public function emptys(){
		   $this->cart->destroy();
		   session_unset('ses_total_service');
		   session_unset('ses_total_rooms');
		   session_unset('ses_total_adults');
		   session_unset('ses_total_childs');
		   session_unset('ses_total_infants');
		   session_unset('ses_services');
		   session_unset('ses_extra_service');
		   session_unset('ses_startdate');
		   session_unset('ses_ratetour');
		   session_unset('ses_cart_price');
		   session_unset('ses_shipid');
			echo "true";
	   }
	   public function order(){
		  if(!$_POST){ die("false!");}
		  $totalservice =  (int)$_POST['totalservice'];
		  $visa = $this->input->post("visa");
		  $totalprice = $this->input->post("totalprice");
		  //die($totalprice);
		  $services = array();
		  for($i=1;$i<=$totalservice;$i++){
			  $key = $this->input->post("service".$i); 
			  $val = $this->input->post("servicetitle".$i); 
			  $typ = $this->input->post("serviceabc".$i);
			  $numberadult=$this->input->post("adulttransfer".$i);
			  $numberchild=$this->input->post("childtransfer".$i);
			  $priceadult=$this->input->post("priceadult".$i);
			  $pricechild=$this->input->post("pricechild".$i);
			  //$typ = "1";
			  if($key != NULL){
				  $services[$key]['type'] = $typ;
				  $services[$key]['val']  = $val;
				  $services[$key]["numberadult"]=$numberadult;
				  $services[$key]["numberchild"]=$numberchild;
				  $services[$key]["priceadult"]=$priceadult;
				  $services[$key]["pricechild"]=$pricechild;
			      //$services[$key] =  array($key => $val);
				  //$services[$key."type"]['type'] = $typ;
			  }
		  }
		  $total_service = count($services);
		  $visanum = "";
		  $total_price_ser = array_sum(array_keys($services));
		  //echo $total_price_ser;
		  //die();
		  $visanum = $this->input->post("visanum");
		  if($visa != NULL){
			  if($this->session->userdata("rate") == "vnd"){
				  $price_visa = $visanum*210000;
			  }else{
			  	$price_visa = $visanum*10;
			  }
		  }else{
			  $price_visa = 0;
			  $visanum = 0;
		  }
		  $cart_price = $this->cart->total();
		  $price_all = $total_price_ser + $price_visa + $cart_price;
		  //echo $price_all;
		  //die();
		  if($totalprice != $price_all){
			  redirect(base_url()."your-cart#infocruise");
		  }else{
			  $cart = $this->cart->contents();
			  //$this->debug($cart);
			  $adults  = array();
			  $childs  = array();
			  $infants = array();
			  $rooms   = array();
			  foreach($cart as $items){
				  $ship_id   = $items['options']['ship']; 
				  $adults[]  = $items['options']['adults'];
				  $childs[]  = $items['options']['child'];
				  $infants[] = $items['options']['infants'];
				  $singler[] = $items['options']['singler'];
				  $doubler[] = $items['options']['doubler'];
				  $twinr[]   = $items['options']['twinr'];
				  $startdate = $items['options']['startdate'];
				  $endate = $items['options']['endate'];
				  $rate_tour = $items['options']['rate_tour'];
				  //break;
			  }
			  $total_adults   = array_sum($adults);
			  $total_childs   = array_sum($childs);
			  $total_infant   = array_sum($infants);
			  $total_singler  = array_sum($singler);
			  $total_doubler  = array_sum($doubler);
			  $total_twinr    = array_sum($twinr);
			  $service_save   = serialize($services);
			  //$this->debug($services);
			  $_SESSION["ses_total_cart"] = serialize($this->cart->contents());
			  $_SESSION["ses_total_service"] = $total_service;
			  $_SESSION["ses_total_singler"] = $total_singler;
			  $_SESSION["ses_total_doubler"] = $total_doubler;
			  $_SESSION["ses_total_twinr"]  = $total_twinr;
			  $_SESSION["ses_total_adults"]  = $total_adults;
			  $_SESSION["ses_total_childs"]  = $total_childs;
			  $_SESSION["ses_total_infants"] = $total_infant;
			  $_SESSION["ses_services"]     = $service_save;
			  $_SESSION["ses_extra_service"] = $visanum;
			  $_SESSION["ses_startdate"]  = $startdate;
			  $_SESSION["ses_endate"]  = $endate;
			  $_SESSION["ses_ratetour"]   = $rate_tour;
			  $_SESSION["ses_cart_price"]  = $price_all;
			  $_SESSION["ses_shipid"] 	= $ship_id;
			  redirect(base_url()."home/booking/book#submitbooking");
		  }
	   }
   }